/*
 * Copyright 2006 - 2007 The MITRE Corporation. All rights reserved.
 * Written by Ayal Spitz
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License and the following two conditions:
 * 
 * The US Government will not be charged any license fee and/or royalties 
 * related to this software.
 * 
 * Neither name of The MITRE Corporation; nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/copyleft/lesser.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.mitre.streamingRSS.xmpp;

import java.util.Hashtable;

import org.jivesoftware.smack.XMPPException;

/**
 * The Subscriber class is designed to simplify the process of subscribing in
 * an XMPP session. It is the base class of both spitz.ayal.irss.Subscriber.
 * This class follows in the footsteps of Ruby on Rails and puts convention
 * over configuration, ie there is an attempt to make smart defaults over long 
 * config files.
 * 
 * @author Ayal Spitz
 *
 */
public class Subscriber extends Common{
// ### Class Fields ###########################################################
	protected Hashtable<String, SubThread>			mucNameToThreadTable = null;
	
// ### Class Constructor ######################################################	
	public Subscriber(){ this("localhost"); }
	
	/**
	 * This constructor connects to the host passed in using the default port
	 * (5222) for connecting to the XMPP server
	 * 
	 * @param host					Either the host name or IP address of the 
	 * 								XMPP server. If "localhost" is passed in
	 * 								the constructor will attempt to turn
	 */
	public Subscriber(String host){ this(host,5222); }

	/**
	 * This constructor connects to the host and port passed for the XMPP server
	 * 
	 * @param host					Either the host name or IP address of the 
	 * 								XMPP server. If "localhost" is passed in
	 * 								the constructor will attempt to turn
	 * @param port					The port to connect to the XMPP server
	 */
	public Subscriber(String host,int port){
		super(host,port);
		
		mucNameToThreadTable = new Hashtable<String, SubThread>();
	}
	

// ### Class Methods ##########################################################
	/**
	 * This method attempts to subscribe to a chat room
	 * 
	 * @param mucName		The name of the chat room to subscribe to
	 * @param subListener	The listener that will listen to the char room
	 * 
	 * @throws XMPPException
	 */
	public void subscribe(String mucName,SubListener subListener)
		throws XMPPException{

		SubThread			subThread = null;

		subThread = mucNameToThreadTable.get(mucName);
		if (subThread == null){
			subThread = new SubThread(this,mucName);
			mucNameToThreadTable.put(mucName,subThread);
		}
		
		subThread.add(subListener);
	}
}